package com.atguigu.gmall.product.api;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.model.*;
import com.atguigu.gmall.product.service.BaseCategoryService;
import com.atguigu.gmall.product.service.BaseTrademarkService;
import com.atguigu.gmall.product.service.SkuManageService;
import com.atguigu.gmall.product.service.SpuManageService;
import org.bouncycastle.asn1.esf.SPUserNotice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 商品模块所有的restful接口，包含/api请求地址都是微服务间内部接口调用
 */
@RestController
@RequestMapping("api/product")
public class ProductApiController {

    @Autowired
    private SkuManageService skuManageService;

    @Autowired
    private BaseCategoryService categoryService;

    @Autowired
    private SpuManageService spuManageService;

    @Autowired
    private BaseTrademarkService baseTrademarkService;


    /**
     * 根据SkuID查询SKU商品信息包含图片列表
     *
     * @param skuId
     * @return
     */
    @GetMapping("/inner/getSkuInfo/{skuId}")
    public SkuInfo getSkuInfo(@PathVariable("skuId") Long skuId) {
        SkuInfo skuInfo = skuManageService.getSkuInfo(skuId);
        return skuInfo;
    }


    /**
     * 通过三级分类id查询分类信息
     *
     * @param category3Id
     * @return
     */
    @GetMapping("inner/getCategoryView/{category3Id}")
    public BaseCategoryView getCategoryView(@PathVariable("category3Id") Long category3Id) {
        return categoryService.getCategoryViewByCategory3Id(category3Id);
    }


    /**
     * 根据商品实时最新价格
     *
     * @param skuId 商品ID
     * @return
     */
    @GetMapping("/inner/getSkuPrice/{skuId}")
    public BigDecimal getSkuPrice(@PathVariable("skuId") Long skuId) {
        BigDecimal price = skuManageService.getSkuPrice(skuId);
        return price;
    }

    /**
     * 根据商品SPUID查询商品海报列表
     *
     * @param spuId 商品SPUID
     * @return
     */
    @GetMapping("/inner/findSpuPosterBySpuId/{spuId}")
    public List<SpuPoster> findSpuPosterBySpuId(@PathVariable("spuId") Long spuId) {
        List<SpuPoster> list = spuManageService.findSpuPosterBySpuId(spuId);
        return list;
    }


    /**
     * 根据SKUID查询当前SKU商品中平台属性以及属性值
     *
     * @param skuId
     * @return
     */
    @GetMapping("/inner/getAttrList/{skuId}")
    public List<BaseAttrInfo> getAttrList(@PathVariable("skuId") Long skuId) {
        List<BaseAttrInfo> list = skuManageService.getAttrList(skuId);
        return list;
    }

    /**
     * 根据SPU_ID,SKU_ID查询所有销售属性，标识SKU选中销售属性
     *
     * @param skuId
     * @param spuId
     * @return
     */
    @GetMapping("/inner/getSpuSaleAttrListCheckBySku/{skuId}/{spuId}")
    public List<SpuSaleAttr> getSpuSaleAttrListCheckBySku(@PathVariable("skuId") Long skuId, @PathVariable("spuId") Long spuId) {
        List<SpuSaleAttr> list = skuManageService.getSpuSaleAttrListCheckBySku(skuId, spuId);
        return list;
    }


    /**
     * 返回所有销售属性对应skuID map集合
     *
     * @param spuId
     * @return
     */
    @GetMapping("/inner/getSkuValueIdsMap/{spuId}")
    public Map getSkuValueIdsMap(@PathVariable("spuId") Long spuId) {
        Map map = skuManageService.getSkuValueIdsMap(spuId);
        return map;
    }


    /**
     * 获取全部分类信息
     *
     * @return
     */
    @GetMapping("/inner/getBaseCategoryList")
    public List<JSONObject> getBaseCategoryList() {
        List<JSONObject> list = categoryService.getBaseCategoryList();
        return list;
    }


    /**
     * 根据品牌ID查询品牌信息
     *
     * @param tmId 品牌ID
     * @return
     */
    @GetMapping("/inner/getTrademark/{tmId}")
    public BaseTrademark getTrademarkById(@PathVariable("tmId") Long tmId) {
        BaseTrademark trademark = baseTrademarkService.getById(tmId);
        return trademark;
    }
}
